package com.vibrationkeyboard.app.ui

import android.os.Bundle
import android.widget.SeekBar
import android.widget.Switch
import androidx.appcompat.app.AppCompatActivity
import com.vibrationkeyboard.app.R
import com.vibrationkeyboard.app.databinding.ActivitySettingsBinding

class SettingsActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySettingsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySettingsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupUI()
        loadSettings()
    }

    private fun setupUI() {
        // 设置标题
        supportActionBar?.title = getString(R.string.title_settings)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 采样频率设置
        binding.seekBarSampleRate.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val sampleRate = 50 + progress * 10 // 50-150 Hz
                binding.tvSampleRateValue.text = "$sampleRate Hz"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                saveSettings()
            }
        })

        // 传感器延迟设置
        binding.seekBarSensorDelay.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val delay = when (progress) {
                    0 -> "最快"
                    1 -> "游戏"
                    2 -> "UI"
                    3 -> "正常"
                    else -> "最慢"
                }
                binding.tvSensorDelayValue.text = delay
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                saveSettings()
            }
        })

        // 振动反馈开关
        binding.switchVibrationFeedback.setOnCheckedChangeListener { _, isChecked ->
            saveSettings()
        }

        // 自动纠错开关
        binding.switchAutoCorrect.setOnCheckedChangeListener { _, isChecked ->
            saveSettings()
        }

        // 声音反馈开关
        binding.switchSoundFeedback.setOnCheckedChangeListener { _, isChecked ->
            saveSettings()
        }

        // 重置按钮
        binding.btnReset.setOnClickListener {
            resetToDefaults()
        }
    }

    private fun loadSettings() {
        val sharedPrefs = getSharedPreferences("vibration_keyboard_settings", MODE_PRIVATE)
        
        // 加载采样频率设置
        val sampleRate = sharedPrefs.getInt("sample_rate", 100)
        val sampleRateProgress = (sampleRate - 50) / 10
        binding.seekBarSampleRate.progress = sampleRateProgress
        binding.tvSampleRateValue.text = "$sampleRate Hz"
        
        // 加载传感器延迟设置
        val sensorDelay = sharedPrefs.getInt("sensor_delay", 1)
        binding.seekBarSensorDelay.progress = sensorDelay
        val delayText = when (sensorDelay) {
            0 -> "最快"
            1 -> "游戏"
            2 -> "UI"
            3 -> "正常"
            else -> "最慢"
        }
        binding.tvSensorDelayValue.text = delayText
        
        // 加载开关设置
        binding.switchVibrationFeedback.isChecked = sharedPrefs.getBoolean("vibration_feedback", true)
        binding.switchAutoCorrect.isChecked = sharedPrefs.getBoolean("auto_correct", false)
        binding.switchSoundFeedback.isChecked = sharedPrefs.getBoolean("sound_feedback", false)
    }

    private fun saveSettings() {
        val sharedPrefs = getSharedPreferences("vibration_keyboard_settings", MODE_PRIVATE)
        val editor = sharedPrefs.edit()
        
        // 保存采样频率
        val sampleRate = 50 + binding.seekBarSampleRate.progress * 10
        editor.putInt("sample_rate", sampleRate)
        
        // 保存传感器延迟
        editor.putInt("sensor_delay", binding.seekBarSensorDelay.progress)
        
        // 保存开关设置
        editor.putBoolean("vibration_feedback", binding.switchVibrationFeedback.isChecked)
        editor.putBoolean("auto_correct", binding.switchAutoCorrect.isChecked)
        editor.putBoolean("sound_feedback", binding.switchSoundFeedback.isChecked)
        
        editor.apply()
    }

    private fun resetToDefaults() {
        // 重置采样频率
        binding.seekBarSampleRate.progress = 5 // 100 Hz
        binding.tvSampleRateValue.text = "100 Hz"
        
        // 重置传感器延迟
        binding.seekBarSensorDelay.progress = 1 // 游戏模式
        binding.tvSensorDelayValue.text = "游戏"
        
        // 重置开关
        binding.switchVibrationFeedback.isChecked = true
        binding.switchAutoCorrect.isChecked = false
        binding.switchSoundFeedback.isChecked = false
        
        // 保存默认设置
        saveSettings()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
} 